/*
 * @Author: DUST
 * @Date: 2023-06-11 16:36:59
 * @LastEditors: DUST
 * @LastEditTime: 2023-06-27 09:31:31
 * @FilePath: \koa-node-ts\app\service\IpService.ts
 * @Description:
 * undefined
 */
import requestApi from "../../utils/request";
import config from "../config";

interface IPdata {
  adcode: {
    a: number;
  };
}

interface WeatherData {
  lives: {
    temperature: string;
    weather: string;
    winddirection: string;
    windpower: string;
    humidity: string;
  };
}

class IpService {
  // https://api.vore.top/api/IPdata 获取ip信息api，请求方式：get,请求参数：ip:需要识别的ip
  getIPInfo(ip: string) {
    return new Promise(async (resolve, reject) => {
      // 使用ip获取位置信息
      const res: IPdata = (await requestApi(
        "get",
        `https://api.vore.top/api/IPdata?ip=${ip}`
      )) as IPdata;
      // 使用位置信息获取天气信息
      const weather: WeatherData = (await requestApi(
        "get",
        `https://restapi.amap.com/v3/weather/weatherInfo?key=${config.AmapKey.appKey}&city=${res.adcode.a}`
      )) as WeatherData;
      // console.log("天气：", weather, config.AmapKey)
      resolve({ ...res, ...weather });
    });
  }
}

export default new IpService();
